package com.jiyun.service;

import com.jiyun.mapper.FaDanMapper;
import com.jiyun.mapper.OrderMapperVo;
import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.entity.Order;
import com.jiyun.pojo.entity.User;
import com.jiyun.pojo.vo.OrderVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FaDanService {
    @Autowired(required = false)
    private FaDanMapper faDanMapper;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private OrderMapperVo orderMapperVo;


    public void Fandan(Order order) {
        order.setStatusid(1);
        order.setBilltime(new Date());
        faDanMapper.insertSelective(order);
    }


    public void updatestatusidQuxiao(Integer oid) {
        Order order = new Order();
        order.setStatusid(4);
        order.setOid(oid);
        faDanMapper.updateByPrimaryKeySelective(order);
    }
    public void songda(Integer oid) {
        Order order = faDanMapper.selectByPrimaryKey(oid);
        order.setStatusid(7);
        faDanMapper.updateByPrimaryKeySelective(order);
        User fdUser = userMapper.selectByPrimaryKey(order.getPtid());
        fdUser.setMoney(fdUser.getMoney()- order.getAmout());
        userMapper.updateByPrimaryKeySelective(fdUser);
        User jdUser = userMapper.selectByPrimaryKey(order.getUserid());
        jdUser.setMoney(jdUser.getMoney()- order.getAmout());
        userMapper.updateByPrimaryKeySelective(jdUser);
    }

    public List<OrderVo> findAllPutongOrder(Integer userid) {
        List<OrderVo> list=orderMapperVo.findAllPutongOrder(userid);
        for (OrderVo orderVo : list) {
            if (orderVo.getPtid()!=null) {
                User user = userMapper.selectByPrimaryKey(orderVo.getPtid());
                orderVo.setUser(user);
                System.out.println(user);
            }
        }
        return list;
    }
    public List<OrderVo> selectstatus(Integer userid,Integer seid) {
        return orderMapperVo.selectstatus(userid,seid);
    }

    public void querenshou(Integer oid,Double amout,Integer userid,Integer ptid) {
        //更改订单状态为
        Order order = new Order();
        order.setOid(oid);
        order.setStatusid(8);
        order.setOrdertime(new Date());
        faDanMapper.updateByPrimaryKeySelective(order);
    }

    public void fukuan(Integer oid, Double amout, Integer userid, Integer ptid) {
        //更改订单状态为
        Order order = new Order();
        order.setOid(oid);
        order.setStatusid(9);
        order.setDeliverytime(new Date());
        faDanMapper.updateByPrimaryKeySelective(order);
        //查找用户所有信息
        User user = userMapper.selectByPrimaryKey(userid);
        Double money = user.getMoney();
        //查找跑腿员信息
        User user1 = userMapper.selectByPrimaryKey(ptid);
        Double money1 = user1.getMoney();
        //把金额给跑腿员
        Double a=money-amout;
        Double b=money1+amout;

        //更改用户
        user.setMoney(a);
        userMapper.updateByPrimaryKeySelective(user);
        user1.setMoney(b);
        userMapper.updateByPrimaryKeySelective(user1);
    }

    public void pingjia(Integer oid, String estimate) {
        Order order = new Order();
        order.setOid(oid);
        order.setStatusid(1);
        order.setEstimate(estimate);
        faDanMapper.updateByPrimaryKeySelective(order);
    }

    public Order findOrderBYid(Integer oid) {
        return faDanMapper.selectOrderByid(oid);
    }

    public void updateBYoid(Order order) {
        System.out.println(order);
        order.setStatusid(1);
        order.setBilltime(new Date());
        faDanMapper.updateByPrimaryKeySelective(order);
    }
}
